#include "stm32f10x.h"
#include "wdtx_timer.h"
#include "wdtx_adc.h"

uint8_t  TIM2CH1_CAPTURE_STA = 0;		    				
uint16_t TIM2CH1_CAPTURE_VAL = 0;	

uint16_t Tim2_GetCapValue()
{
	return TIM2CH1_CAPTURE_VAL;
}

void TIM2_IRQHandler(void)
{ 
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
	{	    
		if(TIM2CH1_CAPTURE_STA == 1)
		{
			TIM2CH1_CAPTURE_VAL = 0xffff;
			TIM2CH1_CAPTURE_STA = 0;
		}
	}
	if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
	{	
		if(TIM2CH1_CAPTURE_STA == 0)
		{
	 		TIM_SetCounter(TIM2,0);
			TIM2CH1_CAPTURE_STA = 1;
		}
		else
		{
			TIM2CH1_CAPTURE_VAL = TIM_GetCapture1(TIM2);
			TIM2CH1_CAPTURE_STA = 0;
		}
	}		     	    					   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); 
}

uint16_t samplePoints[SAMPLE_POINT_SIZE] = {0};
uint8_t next = 0;

uint16_t* TIM3_getSamplePoints(void)
{
	return samplePoints;
}

//@10us
void TIM3_IRQHandler(void)   //TIM3中断
{
	static uint16_t currentValue = 0;
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
	{
		currentValue++;
		if(currentValue == 2000/SAMPLE_POINT_SIZE)
		{
			samplePoints[(next++)%SAMPLE_POINT_SIZE] = Adc_GetSampleValue();
			currentValue = 0;
		}
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx的中断待处理位:TIM 中断源 
	}
}

void TIM2_Cap_Init(void)
{	
	uint16_t arr = 0xffff;
	uint16_t psc = 720 - 1;
	TIM_ICInitTypeDef  TIM2_ICInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);	
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_0);					
	
	TIM_TimeBaseStructure.TIM_Period = arr;
	TIM_TimeBaseStructure.TIM_Prescaler =psc; 
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 
	
	TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1;
  	TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  	TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  	TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 
  	TIM2_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInit(TIM2,&TIM2_ICInitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
	NVIC_Init(&NVIC_InitStructure);  
	
	TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);
	
	TIM_Cmd(TIM2,ENABLE ); 	
}

void TIM3_Int_Init()
{
	uint16_t arr = 10 - 1;
	uint16_t psc = 72 - 1;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能

	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 计数到5000为500ms
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(  //使能或者失能指定的TIM中断
		TIM3, //TIM2
		TIM_IT_Update ,
		ENABLE  //使能
		);
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

	TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设
}

